﻿using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

namespace Azure.Online.Web.Routing {
    public static class RoutesExt {

        public static void MapHttpRoute<T>(this HttpRouteCollection routes, string name, string url, object defaults = null, object constraints = null, string httpVerb = null) where T : ApiController {
            var rd = new RouteValueDictionary(defaults);
            var type = typeof(T);
            rd["controller"] = type.Name.Replace("Controller", string.Empty);
            if (!string.IsNullOrWhiteSpace(httpVerb)) {
                var crd = new RouteValueDictionary(constraints);
                crd["httpMethod"] = new HttpMethodConstraint(httpVerb);
                constraints = crd;
            }

            routes.MapHttpRoute(name, "api/" + url, rd, constraints);
        }

        public static void MapWebRoute<T>(this RouteCollection routes, string name, string url, object defaults = null, object constraints = null, string httpVerb = null) where T : Controller {
            var rd = new RouteValueDictionary(defaults);
            var type = typeof(T);
            rd["controller"] = type.Name.Replace("Controller", string.Empty);
            if (!string.IsNullOrWhiteSpace(httpVerb)) {
                var crd = new RouteValueDictionary(constraints);
                crd["httpMethod"] = new HttpMethodConstraint(httpVerb);
                constraints = crd;
            }

            routes.MapRoute(name, url, rd, constraints);
        }
    }
}